<?php

namespace App\Admin\Controllers;

use App\Models\Schedule;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ScheduleController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.schedule'));
            $content->description(trans('admin.list'));
            
            $content->body($this->grid()->render());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.schedule'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.schedule'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Schedule::class, function (Grid $grid) {
            $grid->model()->where('level','=','1');
            $grid->id(trans('admin.ID'))->sortable();
            $grid->level(trans('admin.level'))->display(function ($level) {
                if($level==1){
                    return trans('admin.department_schedule');
                }else{
                    return trans('admin.personal_schedule');
                }
                
            })->sortable();
            $grid->starttime(trans('admin.schedule_starttime'))->sortable();
            $grid->endtime(trans('admin.schedule_endtime'))->sortable();
            $grid->event(trans('admin.schedule_event'))->display(function($event) {
                return str_limit($event, 30, '...');
            });
            $grid->user_id(trans('admin.add_username'))->display(function($user_id) {
                return Admin::user()->find($user_id)->username;
            })->sortable();
            $grid->status(trans('admin.status'))->editable('select', [0 => '启用', 1 => '禁用'])->sortable();

            $grid->actions(function ($actions) {
                $actions->disableView();
            });

            //禁用导出按钮
            $grid->disableExport();
            

             //筛选弹出框字段
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('level', trans('admin.level'))->select($options = ['1'=> trans('admin.department_schedule') , '2'=> trans('admin.personal_schedule')]);
                    
                    $filter->equal('status', trans('admin.status'))->select($options = ['0'=> trans('admin.enable') , '1'=> trans('admin.disable')]);
                });
                $filter->column(1/2, function ($filter) {
                    // $filter->between('time', trans('admin.time'))->datetime();
                    $filter->equal('user_id', trans('admin.add_username'))->select(Admin::user()->all()->pluck('username', 'id'));
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Schedule::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->select('level', trans('admin.level'))->options(['1'=>trans('admin.department_schedule'),'2'=>trans('admin.personal_schedule')])->default(1)->rules('required')->addElementLabelClass('must-input'); 
            $form->datetime('starttime', trans('admin.schedule_starttime'))->format('YYYY-MM-DD HH:mm:ss')->rules('required')->addElementLabelClass('must-input');
            $form->datetime('endtime', trans('admin.schedule_endtime'))->format('YYYY-MM-DD HH:mm:ss')->rules('required')->addElementLabelClass('must-input');
            $form->textarea('event', trans('admin.schedule_event'))->rows(10);
            $form->select('status', trans('admin.status'))->options(['0'=> trans('admin.enable'),'1'=> trans('admin.disable')])->rules('required')->addElementLabelClass('must-input');
            $form->hidden('user_id')->value(Admin::User()->id);

            //去掉添加/编辑表单的重置按钮
            //$form->disableReset();
        });
    }

    protected function schedule_add_form()
    {
        $date = request('date');
        return view('admin.schedule_add_form',compact('date'));
    }

    protected function schedule_get()
    {
        $my_schedule = Schedule::where('level','=','2')->where('status','=','0')->where('user_id','=',Admin::user()->id)->get()->toArray();
        $dep_schedule = Schedule::where('level','=','1')->where('status','=','0')->get()->toArray();
        $schedule = array_merge($my_schedule,$dep_schedule);
        foreach ($schedule as $key => $value) {
            // $allday = $value['allday'];
            // $is_allday = $allday==1?true:false;
            
            $data[] = array(
                'id' => $value['id'],
                'title' => $value['event'],
                'start' => $value['starttime'],
                'end'   => $value['endtime'],
                'color' => $value['color']
            );
        }
        echo json_encode($data);
    }

    protected function schedule_add_handle()
    {
        $events = stripslashes(trim(request('event')));//事件内容

        $startdate = trim(request('startdate'));//开始日期
        $enddate = trim(request('enddate'));//结束日期

        $colors = array("#360","#f30","#06c");
        $key = array_rand($colors);
        $color = $colors[$key];

        // $isallday = $isallday?1:0;

        $schedule = new Schedule;

        $schedule = Schedule::create([
            'level'     => '2',
            'starttime' => $startdate,
            'endtime'   => $enddate,
            'event'     => $events,
            'color'     => $color,
            'status'    => '0',
            'user_id'   => Admin::user()->id
        ]);

        if($schedule){
            echo '1';
        }else{
            echo '写入失败！';
        }
    }
    protected function schedule_edit_handle()
    {
        $id = request('eventid');
        $events = trim(request('event'));//事件内容

        $schedule = new Schedule;

        $num = Schedule::where('id','=',$id)->update(
           ['event' => $events]
        );
        if($num == 1){
            echo '1';
        }else{
            echo '修改失败！';
        }
    }

    protected function schedule_delete_handle()
    {
        $id = request('eventid');
        $schedule = new Schedule;

        $num = Schedule::destroy($id);
        if($num == 1){
            echo '1';
        }else{
            echo '删除失败！请稍后重试';
        }
    }
}
